package com.lgqz.himalayafm.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.bean.AlbumDetails;
import com.lgqz.himalayafm.bean.RecsListItem;
import com.lgqz.himalayafm.bean.User;
import com.lgqz.himalayafm.http.HttpService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class AlbumDetailsFragment extends Fragment implements Callback<AlbumDetails> {


    private TextView mIntroduce;
    private SimpleDraweeView mAnchorIcon;
    private TextView mAnchorName;
    private TextView mFollower;
    private TextView mSignature;
    private SimpleDraweeView mFirstCover;
    private TextView mFirstTitle;
    private TextView mFirstInfo;
    private TextView mFirstTime;
    private TextView mFirstCount;
    private SimpleDraweeView mSecondCover;
    private TextView mSecondTitle;
    private TextView mSecondInfo;
    private TextView mSecondTime;
    private TextView mSecondCount;
    private SimpleDraweeView mThirdCover;
    private TextView mThirdTitle;
    private TextView mThirdInfo;
    private TextView mThirdTime;
    private TextView mThirdCount;

    public AlbumDetailsFragment() {
        // Required empty public constructor
    }

    public static AlbumDetailsFragment newInstance(long albumId) {
        Bundle args = new Bundle();
        args.putLong("albumId", albumId);
        AlbumDetailsFragment fragment = new AlbumDetailsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        long albumId = getArguments().getLong("albumId");
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service = retrofit.create(HttpService.class);
        service.getAlbumDetails(albumId).enqueue(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_album_details, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mIntroduce = ((TextView) view.findViewById(R.id.tv_introduce_album_detail));
        mAnchorIcon = ((SimpleDraweeView) view.findViewById(R.id.sdv_anchor_icon_album_detail));
        mAnchorName = ((TextView) view.findViewById(R.id.tv_anchor_album_detail));
        mFollower = ((TextView) view.findViewById(R.id.tv_follower_album_detail));
        mSignature = ((TextView) view.findViewById(R.id.tv_signature_album_detail));
        mFirstCover = ((SimpleDraweeView) view.findViewById(R.id.sdv_wonderful_first_album_detail));
        mFirstTitle = ((TextView) view.findViewById(R.id.tv_wonderful_first_title_album_detail));
        mFirstInfo = ((TextView) view.findViewById(R.id.tv_wonderful_first_info_album_detail));
        mFirstTime = ((TextView) view.findViewById(R.id.tv_wonderful_first_times_album_detail));
        mFirstCount = ((TextView) view.findViewById(R.id.tv_wonderful_first_count_album_detail));
        mThirdCover = ((SimpleDraweeView) view.findViewById(R.id.sdv_wonderful_third_album_detail));
        mThirdTitle = ((TextView) view.findViewById(R.id.tv_wonderful_third_title_album_detail));
        mThirdInfo = ((TextView) view.findViewById(R.id.tv_wonderful_third_info_album_detail));
        mThirdTime = ((TextView) view.findViewById(R.id.tv_wonderful_third_times_album_detail));
        mThirdCount = ((TextView) view.findViewById(R.id.tv_wonderful_third_count_album_detail));
        mSecondCover = ((SimpleDraweeView) view.findViewById(R.id.sdv_wonderful_second_album_detail));
        mSecondTitle = ((TextView) view.findViewById(R.id.tv_wonderful_second_title_album_detail));
        mSecondInfo = ((TextView) view.findViewById(R.id.tv_wonderful_second_info_album_detail));
        mSecondTime = ((TextView) view.findViewById(R.id.tv_wonderful_second_times_album_detail));
        mSecondCount = ((TextView) view.findViewById(R.id.tv_wonderful_second_count_album_detail));
    }

    @Override
    public void onResponse(Call<AlbumDetails> call, Response<AlbumDetails> response) {
        mIntroduce.setText(response.body().getData().getDetail().getIntro());
        User user = response.body().getData().getUser();
        mAnchorIcon.setImageURI(user.getSmallLogo());
        mAnchorName.setText(user.getNickname());
        mFollower.setText("已被" + user.getFollowers() + "人关注");
        mSignature.setText(user.getPersonalSignature());

        List<RecsListItem> list = response.body().getData().getRecs().getList();
        mFirstCover.setImageURI(list.get(0).getCoverSmall());
        mFirstTitle.setText(list.get(0).getTitle());
        mFirstInfo.setText(list.get(0).getIntro());
        mFirstTime.setText(list.get(0).getPlaysCounts() + "");
        mFirstCount.setText(list.get(0).getTracks() + "集");

        mSecondCover.setImageURI(list.get(1).getCoverSmall());
        mSecondTitle.setText(list.get(1).getTitle());
        mSecondInfo.setText(list.get(1).getIntro());
        mSecondTime.setText(list.get(1).getPlaysCounts() + "");
        mSecondCount.setText(list.get(1).getTracks() + "集");

        mThirdCover.setImageURI(list.get(2).getCoverSmall());
        mThirdTitle.setText(list.get(2).getTitle());
        mThirdInfo.setText(list.get(2).getIntro());
        mThirdTime.setText(list.get(2).getPlaysCounts() + "");
        mThirdCount.setText(list.get(2).getTracks() + "集");
    }

    @Override
    public void onFailure(Call<AlbumDetails> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
